package cn.ezios.huo.template;


import cn.ezios.huo.biz.sxln.dao.domain.LnMemberCase;
import cn.ezios.huo.system.dao.domain.SysDictCatalog;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import freemarker.core.ParseException;
import freemarker.template.*;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;

public class DtoTemplate {

    private Configuration cfg;

    private final String templateDir = "D:\\workspace\\huo-admin-boot\\Huo-Boot\\src\\test\\resources\\template\\";

    private final String outputDir = "D:\\workspace\\huo-admin-boot\\Huo-Boot\\src\\main\\java\\cn\\ezios\\huo\\dao\\";

    private void init() throws IOException {
        cfg = new Configuration(Configuration.VERSION_2_3_23);
        cfg.setDirectoryForTemplateLoading(new File("D:\\workspace\\huo-admin-boot\\Huo-Boot\\src\\test\\resources\\template"));
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

    }

    @Test
    public void cut() {
        Class<LnMemberCase> clz = LnMemberCase.class;
        Field[] fields = clz.getDeclaredFields();
//        for (Field field : fields) {
//            System.out.println(field.getType().getSimpleName());
//            System.out.println(field.getType().getTypeName());
//        }
        Map<Object, Object> model = MapUtil.builder()
                .put("fields", fields)
                .put("className", clz.getSimpleName())
                .put("TableFieldClz", TableField.class)
                .build();
        try {
            init();
            String outputPath = "D:\\workspace\\java\\huo-admin-boot\\Huo-Boot\\src\\test\\resources\\output\\";
            Template template = cfg.getTemplate("dto.ftl");
            template.process(model, new FileWriter(outputPath + "LnMemberCaseDTO.java"));
        } catch (TemplateException e) {
            throw new RuntimeException(e);
        } catch (TemplateNotFoundException e) {
            throw new RuntimeException(e);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        } catch (MalformedTemplateNameException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void t() throws IOException {
        init();
        ControllerUseTemplate<SysDictCatalog> useTemplate = new ControllerUseTemplate<>();
        useTemplate.setDaoPkg("cn.ezios.huo.dao");
        useTemplate.setClz(SysDictCatalog.class);
        controllerBuild(useTemplate);
        cut(SysDictCatalog.class);
    }

    @SneakyThrows
    public void controllerBuild(ControllerUseTemplate<?> useTemplate) {
        Class<?> clz = useTemplate.getClz();
        String domain = clz.getSimpleName();
        System.out.println("domain:\t" + domain);
        useTemplate.setDomain(domain);
        String domainVarName = Character.toLowerCase(domain.charAt(0)) + domain.substring(1);
        System.out.println("domainVarName:\t" + domainVarName);
        useTemplate.setDomainVarName(domainVarName);
        String symbolCase = StrUtil.toSymbolCase(domain, '-');
        String[] split = symbolCase.split("-");
        String rootPath = "";
        if (split.length < 2) {
            rootPath = symbolCase;
        } else {
            rootPath = StrUtil.subAfter(symbolCase, "-", false);
        }
        useTemplate.setRootPath(rootPath);
        System.out.println("rootPath:\t" + rootPath);

        Template template = cfg.getTemplate(useTemplate.getTemplate());
        template.process(useTemplate,new FileWriter(outputDir + "controller/" + useTemplate.getDomain() + "Controller.java"));
    }


    private boolean cut(Class<?> clz) {
        Field[] fields = clz.getDeclaredFields();
        Map<Object, Object> model = MapUtil.builder()
                .put("fields", fields)
                .put("className", clz.getSimpleName())
                .put("TableFieldClz", TableField.class)
                .build();
        try {
            Template template = cfg.getTemplate("dtoBak.ftl");

            String resultName = clz.getSimpleName() + "DTO.java";
            template.process(model, new FileWriter(outputDir + "dto/" + resultName));
            return true;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
